package com.cmict.data.bpm.service.message;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.bpm.controller.admin.definition.vo.task.TaskVariablePageReqVO;
import com.cmict.data.bpm.convert.message.UserMessageListConvert;
import com.cmict.data.bpm.dal.dataobject.task.TaskCarbonDO;
import com.cmict.data.bpm.dal.mysql.task.TaskCarbonMapper;
import com.cmict.data.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import com.cmict.data.bpm.service.message.dto.TaskCarbonCreateReqDTO;
import com.cmict.data.infra.api.message.MessageSendApi;
import com.cmict.data.infra.api.message.dto.MessageDTO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 流程用户消息 Service 实现类
 *
 * @author liutao
 */
@Service
@Validated
public class UserMessageListServiceImpl implements UserMessageListService {
    @Resource
    private TaskCarbonMapper userMessageListMapper;
    @Autowired
    private MessageSendApi messageSendApi;

    @Override
    public Long createUserMessageList(TaskCarbonCreateReqDTO createReqDTO) {
        // 插入
        TaskCarbonDO userMessageList = UserMessageListConvert.INSTANCE.convert(createReqDTO);
        userMessageListMapper.insert(userMessageList);
        // 返回
        return userMessageList.getId();
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public List<TaskCarbonDO> getUserMessageListList(Long userId) {
       return userMessageListMapper.selectList(TaskCarbonDO::getUserId, userId);
    }

    @Override
    public boolean completeTask(Long carbonId) {
        TaskCarbonDO taskCarbonDO = new TaskCarbonDO();
        taskCarbonDO.setId(carbonId);
        taskCarbonDO.setStatus(true);
        userMessageListMapper.updateById(taskCarbonDO);
        return true;
    }

    @Override
    public int deleteCarbonTask(Long carbonId) {
        return userMessageListMapper.deleteById(carbonId);
    }

    /**
     * 流程驳回提醒
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenTaskRejected(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        this.sendMessage(reqDTO,"流程驳回提醒");
    }

    /**
     * 流程待办提醒
     * @param reqDTO 发送信息
     */
    @Override
    public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
       this.sendMessage(reqDTO,"流程待办提醒");
    }
    /**
     *  流程通过提醒
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenProcessCompleted(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        this.sendMessage(reqDTO,"流程通过提醒");
    }

    /**
     *  流程未通过提醒
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenProcessCancel(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        this.sendMessage(reqDTO,"流程未通过提醒");
    }

    /**
     * 流程抄送提醒
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenTaskCarbon(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        this.sendMessage(reqDTO,"流程抄送提醒");
    }
    private void sendMessage(BpmMessageSendWhenTaskCreatedReqDTO reqDTO, String title) {
        Long  messageTemplateId =   messageSendApi.getTemplateByTitle(title);
        if(messageTemplateId == null){
            return;
        }
        MessageDTO messageDTO=new MessageDTO();
        messageDTO.setTemplateId(messageTemplateId);
        messageDTO.setSendAction(1);
        messageDTO.setRecipientFilterType(2);
        Long assigneeUserId = reqDTO.getAssigneeUserId();
        Long startUserId = reqDTO.getStartUserId();
        assigneeUserId = assigneeUserId == null ? startUserId : assigneeUserId;
        messageDTO.setRecipientFilterType(2);
        messageDTO.setRecipientFilterCondition(Lists.newArrayList(assigneeUserId));
        messageDTO.setUrl("");
        messageDTO.setChannel(reqDTO.getChannel());
        Map<String, Object> params = Maps.newHashMap();
        params.put("processInstanceId", reqDTO.getProcessInstanceId());
        params.put("processInstanceName", reqDTO.getProcessInstanceName());
        params.put("assigned", assigneeUserId);
        params.put("startUser", startUserId);
        messageDTO.setParam(params);

        messageDTO.setIsRemind(reqDTO.getIsRemind());
        messageDTO.setTime(reqDTO.getTime());
        messageDTO.setMessage(reqDTO.getMessage());
        messageDTO.setFrequency(reqDTO.getFrequency());
        messageDTO.setRemindId(reqDTO.getRemindId());
        messageDTO.setCompleterFrequency(reqDTO.getCompleterFrequency());
        messageSendApi.sendMessageByTemplate(messageDTO);
    }
    @Override
    public PageResult<TaskCarbonDO> selectPage(TaskVariablePageReqVO pageReqVO) {
        return userMessageListMapper.selectPage(pageReqVO);
    }

    @Override
    public void batchDeleteCarbon(Collection<Long> ids) {
        userMessageListMapper.deleteBatchIds(ids);
    }

}